<?php

namespace App\Console\Commands;

use App\Http\Model\Hint;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;

class CommandColorGiftRoundChange extends Command
{
    protected $colorGiftPoolTable = 'color_gift_pool';//彩蛋池表
    protected $colorGiftPoolConfigTable = 'color_gift_pool_config';
    protected $colorGiftRoundChangeLog = 'color_gift_pool_round_log';

    protected $colorGiftPoolRateNum = 'COLOR:GIFT:ROUND:POOL:';//对应奖池下的概率次数缓存 COLOR:GIFT:ROUND:POOL:{poolid}
    protected $colorGiftRoundTime = 'COLOR:GIFT:ROUND:LASTTIME:POOL:';//上次更新时间
    protected $colorGiftDrawRedis ='{COLORGIFT:POOLDATA:%s}';
    protected $colorActGiftPoolRate = 'ACTIVITY:COLOR:GIFT:POOLRATE';//string 彩蛋概率列表

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'CommandColorGiftRoundChange';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '轮换奖池概率';

    protected $log = 'color_round';
    protected $error_log = 'error_color_round';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        do{
            //获取目前全部开启的奖池
            $allOpenPool = DB::table($this->colorGiftPoolTable)->where('is_open',1)->get();
            $allOpenPool = objToArray($allOpenPool);        
            if(empty($allOpenPool)){
               write_log($this->log,'INFO 没有开启的奖池', $allOpenPool);
               break; 
            }
             
            //处理逻辑:从奖池开启时间 + 所配的时间段 = 现在时间 切换奖池
            foreach($allOpenPool as $k=>$v){
                //获取对应奖池下配置的概率
                $poolRateArr = DB::table($this->colorGiftPoolConfigTable)
                            ->where('pool_id',$v['id'])
                            ->where('is_delete',0)
                            ->distinct('rate_num')
                            ->get(['rate_num']);
                $poolRateArr = objToArray($poolRateArr); 
                $poolRateArr = array_column($poolRateArr,'rate_num');
                $lastTime = Redis::get($this->colorGiftRoundTime.$v['id']);
                if(empty($lastTime)){
                    $lastTime = strtotime($v['updated_at']); 
                }
                //上次时间加配置间隔 = $now 切换
                $addTime = $lastTime; 
                //时间比较
                $now = time();
                $needTimeOne = date('Y-m-d H:i',$addTime);
                $needTimeTwo = date('Y-m-d H:i',$now);;
                $diffTime = Carbon::parse($needTimeOne)->diffInMinutes($needTimeTwo);
                $hoursTominutes = $v['hours'] * 60;
                
                if($diffTime >= $hoursTominutes && $diffTime <= $hoursTominutes+1){
                    $nowPoolRate = Redis::get($this->colorGiftPoolRateNum.$v['id']);
                    $changePoolRate = $nowPoolRate+1;
                    if(in_array($changePoolRate,$poolRateArr)){
                        $newPoolRate = $changePoolRate;
                    }else{
                        $newPoolRate = 1;
                    }
                    //查看缓存是否存在为了兼容老逻辑
                    $key = sprintf($this->colorGiftDrawRedis, $v['id'].'-'.$newPoolRate);
                    $nameKey = $key . ':ALIAS:' . 'KEYNAME';
                    if(!Redis::EXISTS($nameKey)){
                        write_log($this->log,'INFO 不存在奖池', ['key'=>$key]);
                        continue;
                    }
                    Redis::SET($this->colorGiftPoolRateNum.$v['id'],$newPoolRate);
                    Redis::SET($this->colorGiftRoundTime.$v['id'],$now);
                    Redis::DEL($this->colorActGiftPoolRate);
                    //入库记录
                    $data = [
                        'last_round' => $nowPoolRate,
                        'now_round' => $newPoolRate,
                        'pool_id' => $v['id'],
                        'last_round_time' => date('Y-m-d H:i:s',$lastTime),
                        'now_round_time' => date('Y-m-d H:i:s',$now),
                        'created_at' => date('Y-m-d H:i:s',time()),
                    ];
                    DB::table($this->colorGiftRoundChangeLog)->insert($data);
                }else{
                    write_log($this->log,'INFO 未满足', ['addTime'=>$addTime,'now'=>$now]);
                }
            }
        }while(false);
        
    }
}